package a09_贪心算法;

/**
 * <p>
 * a04_最大子序和复习3
 * </p>
 *
 * @author flyduck
 * @since 2025/4/7
 */
public class a04_最大子序和复习3 {
    public static void main(String[] args) {
        System.out.println(maxSubArray(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4}));
    }
    public static int maxSubArray(int[] nums) {
        int result = Integer.MIN_VALUE;

        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
            result = Math.max(result, sum);
            if(sum < 0){
                sum = 0;
            }
        }
        return result;
    }
}
